﻿var rootUrl = '/courseware';

//判断值是否为空
function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == ""){
        return true;
    }else{
        return false;
    }
}

//短信验证码计时器
function startClock(){
	_count = 60;

	setInterval(function(){

		if(_count == 0){
			$(".btn-getcode").html("获取验证码");
			$(".btn-getcode").attr("disabled", false);
			$(".validation-code").empty();
			return;
		}else{
			_count--;
			$(".btn-getcode").html(_count + "s后重新获取");
		}

	}, 1000);
}


$(document).ready(function(){

	var instance = axios.create({
	  headers: {'X-SS-API-KEY': '8a0471e9-c527-4d57-99a1-6daec32bb629'}
	});

	//登录按钮单击事件
	$(".btn-login").click(function(){
		var _account;
		var _password;
		_account = $("#login-account").val();
		_password = $("#login-password").val();

		instance.post('/eduonline/api/member/login', {
			account : _account,
			password : _password
		}).then(function (response) {
			var _userId = response.data.value.id;
			var _userName = response.data.value.userName;
			var _region = response.data.value.displayName;
			var _account = {id:_userId, userName:_userName, region:_region};
			$.cookie('xwsqxy_account', JSON.stringify(_account), {expires: 30, path: '/'});

			location.href = rootUrl;

		}).catch(function (error){
                        console.log("error:" + JSON.stringify(error.response));
			$("#login-info").text(error.response.data.message).show();
		});
	});

	//获取注册中的短信验证码按钮单击事件
	$(".btn-getcode").click(function(){
		var _mobile;

		_mobile = $("#register-mobile").val();

		instance.get('/aliyunsms/api/send?mobile=' + _mobile)
		.then(function(response){
			$(".btn-getcode").attr("disabled", true);
			console.log(response.data);
			startClock();
			$(".validation-code").val(response.data);
		});

		return false;
	});

	//获取重置密码中的短信验证码按钮单击事件
	$("#btnResetGetCode").click(function(){
		var _mobile;
		var _userName;

		_userName = $("#reset-account").val();

		if(_userName === null || _userName === "" || _userName.length === 0) {
			$("#reset-info").text("请先输入用户名或手机号").show();
			return false;
		}else{
			instance.get('/aliyunsms/api/sendbyaccount?account=' + _userName)
				.then(function(response){
					$(".btn-getcode").attr("disabled", true);
					console.log(response.data);
					startClock();
					$(".validation-code").val(response.data);
				});
		}

		
	});

	//注册按钮单击事件
	$(".btn-register").click(function(){
		var _userName;
		var _password;
		var _displayName;
		var _email;
		var _mobile;
		var _community;

		_userName = $("#register-account").val();
		_password = $("#register-password").val();
		_mobile = $("#register-mobile").val();
		_community = $("#register-community").val();
		_code = $("#register-code").val();

		if(isEmpty(_userName)){
			$("#register-info").text("账号不能为空").show();
			return false;
		}

		if(isEmpty(_password)){
			$("#register-info").text("密码不能为空").show();
			return false;
		}

		if(isEmpty(_mobile)){
			$("#register-info").text("手机号码不能为空").show();
			return false;
		}

		if(isEmpty(_code)){
			$("#register-info").text("验证码不能为空").show();
			return false;
		}

		if(isEmpty(_community)){
			$("#register-info").text("请选择所在的街道").show();
			return false;	
		}

		instance.get('/eduonline/api/member/validatecode?aom=' + _mobile + "&code=" + _code + "&encryptionCode=" + $(".validation-code").val() + "&type=2&pwd=" + _password, {}).then(function (response) {

			if(response.data == "success") {
				
				instance.post('/api/v1/users', {
					userName: _userName,
					password: _password,
					displayName: _community,
					email: _mobile + '@126.com',
					mobile: _mobile
				}).then(function (response) {

					console.log(response.data);
					var _userId = response.data.value.id;
					var _userName = response.data.value.userName;
					var _region = response.data.value.displayName;
					var _account = {id:_userId, userName:_userName, region:_region};
					$.cookie('xwsqxy_account', JSON.stringify(_account), {expires: 30, path: '/'});

					location.href = rootUrl;
					
				}).catch(function (error){
					$("#register-info").text(error.response.data.message).show();
				});

			} else {
				
				$("#register-info").text('短信验证码错误').show();
			}		
	    })
		
	});

	//重置确定按钮单击事件
	$(".btn-reset").click(function(){
		var _userName;
		var _password;
		var _repeatPassword;
		var _code;

		_userName = $("#reset-account").val();
		_password = $("#reset-password").val();
		_repeatPassword = $("#reset-repeatPassword").val();
		_code = $("#reset-code").val();

		if(isEmpty(_userName)){
			$("#reset-info").text("账号不能为空").show();
			return false;
		}

		if(isEmpty(_password)){
			$("#reset-info").text("新密码不能为空").show();
			return false;
		}

		if(isEmpty(_repeatPassword)){
			$("#reset-info").text("未确认新密码").show();
			return false;
		}

		if(!(_password == _repeatPassword)){
			$("#reset-info").text("两次输入的密码不一致").show();
			return false;
		}

		if(isEmpty(_code)){
			$("#reset-info").text("短信验证码不能为空").show();
			return false;
		}

		instance.get('/eduonline/api/member/validatecode?aom=' + _userName + "&code=" + _code + "&encryptionCode=" + $(".validation-code").val() + "&type=1&pwd=" + _password, {}).then(function (response) {
			if(response.data == "success") {
				console.log(response.data);
				$('#reset').children().hide();
				$('#reset-info').html('密码重置成功，请点击 <a href="login.html"><span style="color:#F4B524;">登录</span></a>').show();
				
			} else {
				$("#reset-info").text(response.data).show();
			}
	    })

	});
});